<?php
/*=============================================================================
|| ##################################################################
||	phpFoX Konsort
|| ##################################################################
||
||	Copyright		: (C) 2005-2007 PHPFOX Limited a Reality Fox Creation
||	Contact			: info@phpfox.com
||
||	- phpFoX and all of its source code and files are protected by Copyright Laws.
||
||	- The license for phpFoX permits you to install this software on a single domain (.com, .org, .net, etc.).
||
||	- You may also not remove this copyright screen which shows the copyright information and credits for phpFoX (RealityFOX Creations).
||
||	- phpFoX Konsort is NOT a FREE software - http://www.phpfox.com/license/
||
|| ##################################################################
=============================================================================*/

/* $File: ./include/modules/Site/classes/PhpFox_ComponentArchive.class.php, $Author:  $ */



/** Component for view Archive
 * @version $Id: PhpFox_ComponentArchive.class.php 46 2007-12-14 23:12:21Z phpfoxsv $
 * @package module.site
 */
class PhpFox_Mod_Site_ComponentArchive extends BaseComponent
{
    function pageCode( $str )
	{
		$str = str_replace("&","&amp;",$str);
		$str = strip_tags($str);
		$str = str_replace("\n","<br />",$str);
		return $str;
	}

    function process()
    {
        $nPerPage = 5;
    	$oSecurityService = &App::getModuleService('Account', 'Security');
        $oEventItem = App::getModuleItem('Event', 'Event');
        $oBlogItem = App::getModuleItem('Blog', 'Blog');
        $oGalleryItem = App::getModuleItem('Gallery', 'Gallery');
        $oForumItem = App::getModuleItem('Forum', 'Forum');
        $aSections = array(
			'blog' => array(
				'link' => 'blog',
				'url' => 'public.blog.view',
				'url_main' => App::makeUrl('public.blog.main'),
                'key' => 'id',
                'key_val' => 'title',
                'item_val' => 'id',
				'dbName' => App::getT('journal'),
				'dbQuery' => '',
				'dbTitle' => 'title',
				'dbItem' => 'WHERE `id` = \'%s\'',
				'dbItemDisplay' => array(
						''. App::format('title') .'|title',
						''. App::format('Mod_Tag.tags') .'|tags',
						''. App::format('blog_entry') .'|text',
						),
				'dbOrder' => 'time',
				'perm' => 'blog',
				'access' => 'blogs'
			),
			'event' => array
			(
			    'link'          => 'event',
			    'url'           => 'public.event.view',
			    'url_main'      => App::makeUrl('public.event'),
			    'key'           => 'id',
			    'item_val'      => 'id',
			    'key_val'       => 'title',
			    'dbName'        => App::getT('events'),
			    'dbQuery'       => '',
			    'dbTitle'       => 'title',
			    'dbItem'        => 'WHERE `id` = \'%s\'',
			    'dbItemDisplay' => array
			    (
			        ''. App::format('title') .'|title',
			        ''. App::format('description') .'|text',
			    ),
			    'dbOrder'       => 'time',
			    'perm' => 'events',
			    'access' => 'events'
			),
			'member' => array(
				'link' => 'member',
				'url' => 'public.user',
				'url_main' => App::makeUrl('public.browse'),
    			'key' => 'name',
    			'key_val' => 'user',
    			'item_val' => 'user',
				'dbName' => App::getT('user'),
				'dbQuery' => '',
				'dbTitle' => 'user',
				'dbItem' => 'WHERE `id` = \'%s\'',
				'dbItemDisplay' => array(
						''. App::format('username') .'|user',
						''. App::format('gender') .'|gender',
						''. App::format('about_me_355') .'|me',
						),
				'dbOrder' => 'signup',
				'perm' => 'profile',
				'access' => ''
			),
			'gallery' => array(
				'link' => 'gallery',
				'url' => 'public.gallery.view',
				'url_main' => App::makeUrl('public.gallery.main'),
    			'key' => 'id',
    			'key_val' => 'text',
    			'item_val' => 'id',
				'dbName' => App::getT('main'),
				'dbQuery' => '',
				'dbTitle' => 'text',
				'dbItem' => 'WHERE `id` = \'%s\'',
				'dbItemDisplay' => array(
						''. App::format('title') .'|text',
						''. App::format('Mod_Tag.tags') .'|tags',
						),
				'dbOrder' => 'time',
				'perm' => 'gallery',
				'access' => 'gallery'
			),
			'forum' => array(
				'link' => 'forum',
				'url' => 'public.forum.posts',
				'url_main' => App::makeUrl('public.forum'),
			    'key' => 'id',
			    'item_val' => (App::pluginInstalled('vbulletin') ? 'threadid' : 'id'),
			    'key_val' => 'title',
				'dbName' => (App::pluginInstalled('vbulletin') ? VB_PREFIX . 'thread' : App::getT('forum_topic')),
				'dbQuery' => '',
				'dbTitle' => 'title',
				'dbItem' => (App::pluginInstalled('vbulletin') ? 'WHERE f.`threadid` = \'%s\'' : 'WHERE `id` = \'%s\''),
				'dbItemDisplay' => array(
						''. App::format('subject') .'|title',
						''. App::format('content') .'|'. (App::pluginInstalled('vbulletin') ? 'pagetext' : 'text') .'',
						),
				'dbOrder' => (App::pluginInstalled('vbulletin') ? 'dateline' : 'time'),
				'perm' => 'forum',
				'access' => 'forum'
			),
		);

		$oReq = &App::getRequest();
        $oUrl = &App::getUrl();

        $oTpl = &$this->_oModule->getTemplate();
        $sAct = 'full';

        $nPage = $oReq->getInt('page',0);
        $nId = $oReq->getInt('id',0);
        $sModule = $oReq->get('mod','');

        if($nId == 0 && $sModule == '')
            $sAct = 'full';
        if($nId == 0 && $sModule != '')
            $sAct = 'partial';
        if($nId != 0 && $sModule != '')
            $sAct = 'item';

        $sSlogan = App::getSetting('site_title');
        $aData = array();

	    $oItem = $this->_oModule->getItem('Template');
	    $aHeader = array();
	    $aHeader[] = array('url'=>App::makeUrl('public.archive'),'title'=>$sSlogan);
	    $bShowPrevPage = false;
	    $bShowNextPage = false;
	    $nNextPage = 0;
	    $nPrevPage = 0;

	    if ( ($sUserName = $oReq->get('title')) && $oReq->get('mod') == 'member' )
	    {
	    	App::gotoUrl('public.user', array('name' => $sUserName));
	    }

        if($sAct == 'full')
        {
            foreach($aSections as $name => $value)
			{
        		if ( !$oSecurityService->checkPerm($value['perm'], array('read_only', 'full'), false) || (!empty($value['access']) && !$oSecurityService->checkAccess($value['access'], '', false)) )
        		{
            		continue;
        		}
			    $aItem = array();
			    $aItem['url'] = App::makeUrl('public.archive',array('mod'=>$name));
			    $aItem['title'] = ucfirst($name);
	            $aItem['elements'] = array();

				if ( (App::pluginInstalled('vbulletin') && $name == 'forum') )
				{
					$sSql = "
						SELECT t.`threadid` as id, t.`postusername` as user, t.`title`, t.`dateline` as time
						FROM `". VB_PREFIX ."thread` AS t
							LEFT JOIN `". VB_PREFIX ."forum` AS f
							ON t.forumid = f.forumid
						WHERE t.`visible` = '1' AND f.showprivate != '1'
						ORDER BY t.`dateline` DESC LIMIT 0,20
					";
				}
				else
				{
	            	$sSql = "SELECT * FROM `". $value['dbName'] ."` ORDER BY `". $value['dbOrder'] ."` DESC LIMIT 0,20";
	            }
				$aRes = $oItem->oDb->getRows($sSql);
				foreach($aRes as $aVal)
				{
				    $aElement = array();
				    $title = empty($aVal[$value['dbTitle']]) ? 'No Title' : $aVal[$value['dbTitle']];
                    $title = ucwords(strtolower($this->pageCode($title)));
				    $aElement['title'] = html_entity_decode($title);
				    $aElement['url'] = App::makeUrl('public.archive',array('mod'=>$name,'id'=>$aVal[$value['item_val']],'title'=>App::cleanUrl($title)));
				    $aItem['elements'][] = $aElement;
				}
				$aData[] = $aItem;
			}
			$sFullLink = App::makeUrl('public', array());
        }
        if($sAct == 'partial')
        {
        	$oSecurityService->checkPerm($aSections[$sModule]['perm'], array('read_only', 'full'));
        	(!empty($aSections[$sModule]['access']) ? $oSecurityService->checkAccess($aSections[$sModule]['access']) : '');

                $aHeader[] = array('url'=>App::makeUrl('public.archive', array('mod'=>$sModule)),'title'=>ucfirst($sModule));

				$num = 0;
				$pageNum = $num == 0 ? ($num+1) : $pageNum;

				if ($nPage)
				{
					$pageNum = $nPage;
					$num = (($pageNum-1)*$nPerPage);
				}

	            $aRes = $oItem->oDb->getRow("SELECT COUNT(*) as count FROM `". $aSections[$sModule]['dbName'] ."`");
				$totalCnt = $aRes['count'];
				if ( ($pageNum-1) > 0 )
				{
					$bShowPrevPage = true;
					$nPrevPage = $pageNum-1;
				}
				if ( $totalCnt > ($num+$nPerPage) )
				{
				    $bShowNextPage = true;
				    $nNextPage = $pageNum+1;
				}
				$aRes =  $oItem->oDb->getRows("SELECT * FROM `". $aSections[$sModule]['dbName'] ."` ORDER BY `". $aSections[$sModule]['dbOrder'] ."` DESC LIMIT ". $num .",". $nPerPage ."");
                if(empty($aRes))
                    App::gotoUrl('public.archive', array());

				foreach($aRes as $aVal)
				{
				    $aElement = array();
				    $title = empty($aVal[$aSections[$sModule]['dbTitle']]) ? 'No Title' : $aVal[$aSections[$sModule]['dbTitle']];
				    $title = empty($title) ? 'No Title' : $title;
				    $aElement['title'] = html_entity_decode($title);
                    $aElement['url'] = App::makeUrl('public.archive',array('mod'=>$sModule,'id'=>$aVal[$aSections[$sModule]['item_val']],'title'=>App::cleanUrl($title)));
					$aItem['elements'][] = $aElement;
				}
				$aData[] = $aItem;
				$sFullLink = $aSections[$sModule]['url_main'];
        }
        if($sAct == 'item')
        {
        	$oSecurityService->checkPerm($aSections[$sModule]['perm'], array('read_only', 'full'));
        	(!empty($aSections[$sModule]['access']) ? $oSecurityService->checkAccess($aSections[$sModule]['access']) : '');

                $aHeader[] = array('url'=>App::makeUrl('public.archive', array('mod'=>$sModule)),'title'=>ucfirst($sModule));

                $aRes =  $oItem->oDb->getRow("SELECT * FROM `". $aSections[$sModule]['dbName'].'`'. ((App::pluginInstalled('vbulletin') && $sModule == 'forum') ? ' AS f LEFT JOIN '. VB_PREFIX .'post AS p ON f.`threadid` = p.`threadid` ' : '') .''.sprintf($aSections[$sModule]['dbItem'], $nId));
                if(empty($aRes))
                     App::gotoUrl('public.archive', array());

                $title = empty($aRes[$aSections[$sModule]['key_val']]) ? 'No Title' : $aRes[$aSections[$sModule]['key_val']];
                $aHeader[] = array('url' => $oUrl->getUrl(), 'title' => $title);

				// Display the page title
	            $aElements = array();
				foreach($aSections[$sModule]['dbItemDisplay'] as $mainDisplay)
				{
					$eItem = explode("|",$mainDisplay);
                    $aElements[] = array('name'=>$eItem[0], 'val'=>$aRes[$eItem[1]]);

				}
				$aData['elements'] = $aElements;
				$sFullLink = App::makeUrl($aSections[$sModule]['url'], array($aSections[$sModule]['key']=>$aRes[$aSections[$sModule]['item_val']], 'title' => App::cleanUrl($title)));
        }
        $oTpl->assignSrc(array(
        	'sModule' => $sModule,
        	'bShowNextPage' => $bShowNextPage,
        	'bShowPrevPage' => $bShowPrevPage,
        	'nNextPage' => $nNextPage,
        	'nPrevPage' => $nPrevPage,
        	'sSlogan' => $sSlogan,
        	'aHeader' => $aHeader,
        	'sAct' => $sAct,
        	'aData' => $aData,
        	'sFullLink' => $sFullLink,
        ));
        return $oTpl->fetch('Archive.html');
    }
}
?>